package com.ruoyi.web.task;

import com.corundumstudio.socketio.SocketIOClient;
import com.ruoyi.common.core.domain.MessageInfo;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.web.domain.mes.MesMachineCaredetail;
import com.ruoyi.web.mapper.MesMachineCaredetailMapper;

import static com.ruoyi.framework.socket.SocketIOServiceImpl.clientMap;

@Component("mesCareTask")
public class MesCareTask {

    private final Logger logger = LoggerFactory.getLogger(MesCareTask.class);

    @Resource
    private MesMachineCaredetailMapper mesMachineCaredetailMapper;

    /**
     * 保养详情状态检测
     */
    @Transactional
    public void syncCheckState() {
        // 检测超时
        MesMachineCaredetail machineCaredetail = new MesMachineCaredetail();
        Map<String, Object> map = new HashMap<>();
        map.put("timeOutCheck", "ok"); // 查询不是停用、超时的保养记录
        machineCaredetail.setParams(map);
        List<MesMachineCaredetail> list = mesMachineCaredetailMapper.selectMesMachineCaredetailList(machineCaredetail);
        List<Long> listCaredetailIds = new ArrayList<>();
        for (MesMachineCaredetail item : list) {
            if (item.getEndTime() == null) {
                if (item.getStartTime().compareTo(DateUtils.getNowDate()) < 0) {
                    listCaredetailIds.add(item.getId());
                }
            }
        }
        if (listCaredetailIds.size() > 0) {
            mesMachineCaredetailMapper.updateMesMachineCaredetailByIds(4, listCaredetailIds.toArray(new Long[0]));
        }

    }

    /**
     * 保养详情状态检测(今天)
     */
    @Transactional
    public void syncCheckStateToday() {
        // 检测未完成
        MesMachineCaredetail machineCaredetail = new MesMachineCaredetail();
        Map<String, Object> map = new HashMap<>();
        map.put("timeOutCheck", "ok"); // 查询不是停用、超时的保养记录
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateNow = new Date();
        String timeFormat = dateFormat.format(dateNow);
        map.put("todayCheck", timeFormat); // 查询今天的保养记录
        machineCaredetail.setParams(map);
        List<MesMachineCaredetail> list = mesMachineCaredetailMapper.selectMesMachineCaredetailList(machineCaredetail);
        List<Long> listCaredetailIds = new ArrayList<>();
        for (MesMachineCaredetail item : list) {
            if (item.getEndTime() == null) {
                listCaredetailIds.add(item.getId());
            }
        }
        if (listCaredetailIds.size() > 0) {
            mesMachineCaredetailMapper.updateMesMachineCaredetailByIds(4, listCaredetailIds.toArray(new Long[0]));
        }
    }

    /**
     * 机台库存预警
     */
    @Transactional
    public void syncEarlyWarningInventory() {
        SocketIOClient socketIOClient = clientMap.get("1");
        if (socketIOClient != null) {
            socketIOClient.sendEvent("res", new MessageInfo("1", "admin", "上线！"));
            //log.debug("触发消息推送");
        } else {
            //log.debug("推送用户不在线！");
        }
    }
}
